/**
 * jgamebox
 *
 * outlook@China software studio
 * Copyright (c) 2008-2010. All Rights Reserved.
 * http://www.outlook.com
 */

package org.outlook.jgamebox.map;

import java.io.Serializable;

import org.outlook.common.data.ListMap;
import org.outlook.common.measure.Direction;
import org.outlook.common.measure.Position;
import org.outlook.common.measure.Volume;
import org.outlook.jgamebox.role.Spriter;

/**
 * function:
 * @author Watson email: watson.wu@hotmail.com
 * @date 2011
 */
public class Scene implements Serializable{
	
	private static final long serialVersionUID = -1950306343390367423L;

	private static Volume volume = new Volume();
	
	private ListMap<Spriter, Object> spriters = new ListMap<Spriter, Object>();
	
	public static Volume getVolume() {
		return volume;
	}

	public static void setVolume(Volume vol) {
		volume = vol;
	}
	
	public int getWidth() {
		if(volume == null)
			return 0;
		else
			return volume.getWeight();
	}

	public int getHeight() {
		if(volume == null)
			return 0;
		else
			return volume.getHeight();
	}
	
	public int getThick() {
		if(volume == null)
			return 0;
		else
			return volume.getThick();
	}
	
	public boolean isBeyondMap(Spriter spriter) {
		if(spriter.getPositionX()-spriter.getWidth()<0
				|| spriter.getPositionY()-spriter.getHeight()<0
				|| spriter.getPositionZ()-spriter.getThick()<0)
			return true;
		return false;
	}


	public ListMap<Spriter, Object> getSpriters() {
		return spriters;
	}
	
	public boolean add(Spriter spriter) {
		if(spriters.containsKey(spriter))
			return false;
		spriters.put(spriter, null);
		return true;
	}

	public boolean remove(Spriter spriter) {
		if(!spriters.containsKey(spriter))
			return false;
		spriters.remove(spriter);
		return true;
	}
	
	public boolean isHit(Spriter spriter) {
		for(Spriter sp : spriters.getKeys()) {
			if(isHit(spriter, sp))
				return true;
		}
		return false;
	}

	public static boolean isHit(Spriter sp1, Spriter sp2) {
		Volume volume = Volume.combine(sp1.getVolume(), sp2.getVolume());
		Position distance = Position.distance(sp1.getPosition(), sp2.getPosition());
		if(volume.getWeight()>Math.abs(distance.getX()) && volume.getHeight()>Math.abs(distance.getY()) && volume.getThick()>Math.abs(distance.getZ()))
			return true;
		return false;
	}
	
	public boolean isSpriterExisting(Position position) {
		for(Spriter spriter : spriters.getKeys()) {
			Volume volume = spriter.getVolume();
			Position distance = Position.distance(spriter.getPosition(), position);
			if(volume.getWeight()>Math.abs(distance.getX()) && volume.getHeight()>Math.abs(distance.getY()) && volume.getThick()>Math.abs(distance.getZ()))
				return true;
		}
		return false;
	}

	public boolean isBlock(Spriter spriter, Direction direction) {
		Position position = Direction.move(spriter.getPosition(), spriter.getSpeed(), direction);
		
		return isSpriterExisting(position);
	}

	public boolean isBlock(Spriter spriter) {
		return isBlock(spriter, spriter.getDirection());
	}

}
